﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.Linq;
using System.Collections;
namespace WebApplication4
{

    public partial class Cart : System.Web.UI.Page
    {
        string[] array = new string[100];
        string Id;
        string Name;
        string Brand;
        string Category;
        double Price;
        string Image;
        double total;
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\KiNG1\documents\visual studio 2010\Projects\WebApplication4\WebApplication4\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
            int j = 0;
            int i = 0;

        
            int c;
            dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Brand");
            dt.Columns.Add("Category");
            dt.Columns.Add("Price");
            dt.Columns.Add("Quantity");

    
            for (i = 0; i < Default.j; i++)
            {


                if (Session["id" + j] != null)
                {
                    dt.Rows.Add();
                    dt.Rows[i]["Id"] = (string)(Session["id" + j]);
                    dt.Rows[i]["Name"] = (string)(Session["name" + j]);
                    dt.Rows[i]["Brand"] = (string)(Session["brand" + j]);
                    dt.Rows[i]["Category"] = (string)(Session["category" + j]);
                    dt.Rows[i]["Price"] = (string)(Session["price" + j]);
                    dt.Rows[i]["Quantity"] = (string)(Session["quantity" + j]);


                }
             
                j++;
            }
            

            GridView2.DataSource = dt;
            GridView2.DataBind();
            int[] array = new int[100];
            int[] arr = new int[100];
            double t = 0;
            using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\KiNG1\Documents\Visual Studio 2010\Projects\WebApplication4\WebApplication4\App_Data\Database1.mdf;Integrated Security=True;User Instance=True"))
            {
                int v = 0;
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Products", con);
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    arr[v] = Convert.ToInt32(dr.GetString(0));
                    array[v] =Convert.ToInt32( dr.GetString(6));
                    v++;
                }
                con.Close();
            }
            using (SqlConnection cc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\KiNG1\Documents\Visual Studio 2010\Projects\WebApplication4\WebApplication4\App_Data\Database1.mdf;Integrated Security=True;User Instance=True"))
            {
                cc.Open();
                for (int g = 0; g < GridView2.Rows.Count&&Session["id"+g]!=null ; g++)
                {
                    double f = Convert.ToDouble(GridView2.Rows[g].Cells[4].Text);
                    double qq = Convert.ToDouble(GridView2.Rows[g].Cells[5].Text);
                    f = f*qq;
                    t = t + f;
                    string q = GridView2.Rows[g].Cells[5].Text;
                    for (int m = 0; m < arr.Length; m++)
                    {
                        int id = Convert.ToInt32(GridView2.Rows[g].Cells[0].Text);
                        int qty = Convert.ToInt32(GridView2.Rows[g].Cells[5].Text);
                        if (arr[m] == id)
                        {
                            array[m] = array[m] - qty;

                            SqlCommand cmd = new SqlCommand("Update Products set Quantity = '"+array[m]+"' where Id = '"+id+"'", cc);
                            cmd.ExecuteNonQuery();
                        }
                    }

                }

            }
            Label1.Text = t.ToString(); ;
            

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
         
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void GridView2_deleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView2.Rows[e.RowIndex].Cells[0].Text;
           
            for (int i = 0; i < Default.j; i++)
            {
                if (!(Session["id" + i] == null))
                {
                    string iid = Session["id" + i].ToString();
                    if (iid.Equals(id))
                    {
                        Session.Remove("id" + i);
                        Session.Remove("name" + i);
                        Session.Remove("brand" + i);
                        Session.Remove("category" + i);
                        Session.Remove("price" + i);
                        Session.Remove("quantity" + i);
                    }
                }
            }
            if (Convert.ToInt32(Label1.Text) == 0)
            {
                Session.Clear();
            }

          /*dt.Rows[e.RowIndex].Delete();
            GridView2.DataSource = dt;
            GridView2.DataBind();
             */
            Page_Load(this, null);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int[] array = new int[100];
            int[] arr = new int[100];
            
            using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\KiNG1\Documents\Visual Studio 2010\Projects\WebApplication4\WebApplication4\App_Data\Database1.mdf;Integrated Security=True;User Instance=True"))
            {
                int v = 0;
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Products", con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    arr[v] = Convert.ToInt32(dr.GetString(0));
                    array[v] = Convert.ToInt32(dr.GetString(6));
                    v++;
                }
                con.Close();
            }
            using (SqlConnection cc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\KiNG1\Documents\Visual Studio 2010\Projects\WebApplication4\WebApplication4\App_Data\Database1.mdf;Integrated Security=True;User Instance=True"))
            {
                cc.Open();
                for (int g = 0; g < GridView2.Rows.Count; g++)
                {

                    string q = GridView2.Rows[g].Cells[5].Text;
                    for (int m = 0; m < arr.Length; m++)
                    {
                        int id = Convert.ToInt32(GridView2.Rows[g].Cells[0].Text);
                        int qty = Convert.ToInt32(GridView2.Rows[g].Cells[5].Text);
                        if (arr[m] == id)
                        {
                            array[m] = array[m] - qty;

                            SqlCommand cmd = new SqlCommand("Update Products set Quantity = '" + array[m] + "' where Id = '" + id + "'", cc);
                            cmd.ExecuteNonQuery();
                        }
                    }

                }

            }
        }
    }
}